/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "UserInterface.h"

#include "Card.h"
#include "Logger.h"
#include "Map.h"
#include "Robot.h"

namespace robo
{

    UserInterface::UserInterface(Robot* robot)
    :	RobotController(robot)
    {
        for (int idx = 0; idx < 5; ++idx)
        {
            mSelectedCard[idx] = NULL;
        }
    }

    UserInterface::~UserInterface()
    {
    }

    void UserInterface::setMap(Map* map)
    {
        mMap = map;
    }

    void UserInterface::setAvailableCards(const CardSet& availCards)
    {
        mAvailableCards = availCards;
    }

    void UserInterface::addAvailableCard(Card* card)
    {
        if (mAvailableCards.find(card) != mAvailableCards.end())
	    {
		    return;
	    }

	    for (int idx = 0; idx < 5; ++idx)
        {
            if (mSelectedCard[idx] == card)
            {
			    mSelectedCard[idx] = NULL;
			    break;
            }
        }

        mAvailableCards.insert(card);
	    checkOkButton();
    }

    void UserInterface::removeAvailableCard(Card* card)
    {
        CardSet::iterator it = mAvailableCards.find(card);
            
        if (it != mAvailableCards.end())
        {
		    mAvailableCards.erase(it);
        }
    }

    void UserInterface::setActiveCard(int num, Card* card)
    {
	    if (mSelectedCard[num]) 
	    {
		    // switch cards

		    bool switchSuccessful = false;

		    for (int idx = 0; idx < 5; ++idx)
		    {
			    if (mSelectedCard[idx] == card)
			    {
				    mSelectedCard[idx] = mSelectedCard[num];
				    switchSuccessful = true;
				    break;
			    }
		    }

		    if (!switchSuccessful)
		    {
			    addAvailableCard(mSelectedCard[num]);
		    }
	    }
	    else
	    {
		    for (int idx = 0; idx < 5; ++idx)
		    {
			    if (mSelectedCard[idx] == card)
			    {
				    mSelectedCard[idx] = NULL;
				    break;
			    }
		    }
	    }


	    removeAvailableCard(card);
	    mSelectedCard[num] = card;
	    checkOkButton();
    }

    Card* UserInterface::getActiveCard(int num)
    {
	    return mSelectedCard[num];
    }


    void UserInterface::clear() 
    {
	    ///@todo move deletion of cards to central card management
	    for (int i=0; i<5; ++i)
	    {
		    delete mSelectedCard[i];
		    mSelectedCard[i] = NULL;
	    }

	    for (CardSet::iterator it = mAvailableCards.begin();
            it != mAvailableCards.end(); ++it)
        {
		    delete *it;
	    }
	    mAvailableCards.clear();
    }

}
